package com.example.homework_4_permission.config;

import cn.dev33.satoken.stp.StpInterface;
import com.example.homework_4_permission.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 自定义权限验证接口扩展
 */
@Component    // 保证此类被SpringBoot扫描，完成Sa-Token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private UserMapper userMapper;

    /**
     * 返回一个账号所拥有的权限码集合 不做要求
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        return new ArrayList<>();
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        int uid = Integer.parseInt(loginId.toString());
        return new ArrayList<>(Collections.singletonList(
                userMapper.selectByPrimaryKey(uid)
                        .getRole()));
    }

}
